package cn.itcast.mq.listener;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;


//将当前类放到SpringIOC容器中
@Component
public class SpringRabbitListener {
//    /**
//     * workqueue模式
//     */
//
//    //监听名称simple.queue队列，Spring只要接收到该队列的消息就会接收消息
//    @RabbitListener(queues = "simple.queue")
//    //Spring自动将接收的消息给方法参数msg
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
//        System.out.println("spring 消费者1接收到消息：【" + msg + "】");
//        Thread.sleep(10);
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    //Spring自动将接收的消息给方法参数msg
//    public void listenSimpleQueueMessage2(String msg) throws InterruptedException {
//        System.out.println("spring 消费者2接收到消息：【" + msg + "】");
//        Thread.sleep(100);
//    }
//
//
//    /**
//     * fanout模式
//     */
//    @RabbitListener(queues = "fanout.queue1")
//    public void listenFanoutQueue1(String msg) {
//        System.out.println("消费者1接收到Fanout1消息：【" + msg + "】");
//    }
//
//    @RabbitListener(queues = "fanout.queue1")
//    public void listenFanoutQueue3(String msg) {
//        System.out.println("消费者3接收到Fanout1消息：【" + msg + "】");
//    }
//
//    @RabbitListener(queues = "fanout.queue2")
//    public void listenFanoutQueue2(String msg) {
//        System.out.println("消费者2接收到Fanout2消息：【" + msg + "】");
//    }

    /**
     * direct模式
     */
//    @RabbitListener(bindings = @QueueBinding
//            (value = @Queue(name = "direct.queue1"),
//            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
//            key = {"red", "blue"}
//    ))
//    public void listenDirectQueue1(String msg) {
//        System.out.println("消费者接收到direct.queue1的消息：【" + msg + "】");
//    }
//
//    @RabbitListener(bindings = @QueueBinding
//            (value = @Queue(name = "direct.queue2"),
//                    exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
//                    key = {"red", "yellow"}
//            ))
//    public void listenDirectQueue2(String msg) {
//        System.out.println("消费者接收到direct.queue2的消息：【" + msg + "】");
//    }

    @RabbitListener(queues = "simple.queue")
   public void listenSimoleQueueMessage(String msg) throws InterruptedException {
        System.out.println("消费者1 接收到的消息 :"  + msg);
        Thread.sleep(10);

   }

    @RabbitListener(queues = "simple.queue")
    public void listenSimoleQueueMessage2(String msg) throws InterruptedException {
        System.out.println("消费者2 接收到的消息 :"  + msg);
        Thread.sleep(100);
    }


    @RabbitListener(queues = "zhenglei.fanout1")
    public void fanoutQueueMessage1(String msg)  {
        System.out.println("fanout1 消费者1 接收到的消息 :"  + msg);
    }

    @RabbitListener(queues = "zhenglei.fanout2")
    public void fanoutQueueMessage2(String msg){
        System.out.println("fanout2  消费者2 接收到的消息 :"  + msg);

    }


    @RabbitListener(queues = "zhenglei.fanout1")
    public void fanoutQueueMessage3(String msg){
        System.out.println("fanout3  消费者3 接收到的消息 :"  + msg);

    }


    @RabbitListener(bindings = @QueueBinding(  //绑定
            value = @Queue("direct.queue1"),   //队列
            exchange = @Exchange(value = "zhenglei.direct",type = ExchangeTypes.DIRECT),  //交换机
            key = {"bule","red"} //绑定的路由key
    ))
    public void listenDirectQueueMessage1(String msg){
        System.out.println("direct 模式，消费1 接收到的信息：" + msg);

    }


    @RabbitListener(bindings = @QueueBinding(  //绑定
            value = @Queue("direct.queue2"),   //队列
            exchange = @Exchange(value = "zhenglei.direct",type = ExchangeTypes.DIRECT),  //交换机
            key = {"yellow","red"} //绑定的路由key
    ))
    public void listenDirectQueueMessage2(String msg){
        System.out.println("direct 模式，消费2 接收到的信息：" + msg);

    }




    @RabbitListener(bindings = @QueueBinding(  //绑定
            value = @Queue("topic.queue1"),   //队列
            exchange = @Exchange(value = "zhenglei.topic",type = ExchangeTypes.TOPIC),  //交换机
            key = {"china.#"} //绑定的路由key
    ))
    public void listenTopicQueueMessage1(String msg){
        System.out.println("topic 模式，消费1 接收到的信息：" + msg);

    }


    @RabbitListener(bindings = @QueueBinding(  //绑定
            value = @Queue("topic.queue2"),   //队列
            exchange = @Exchange(value = "zhenglei.topic",type = ExchangeTypes.TOPIC),  //交换机
            key = {"#.news"} //绑定的路由key
    ))
    public void listenTopicQueueMessage2(String msg){
        System.out.println("topic 模式，消费2 接收到的信息：" + msg);

    }


    @RabbitListener(queues = "object.queue")
    public void objectQueueMessage(Map<String,Object> msg){
        System.out.println("object  消费者object 接收到的消息 :"  + msg);

    }



}
